package com.jch.sc.client;

import com.alibaba.fastjson.JSON;
import com.jch.sc.future.SyncWriteFuture;
import com.jch.sc.future.SyncWriteMap;
import com.jch.sc.msg.Response;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class ClientHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Response res = (Response) msg;
//        System.out.println("Response Info: " + JSON.toJSONString(res));
        String requestId = res.getRequestId();
        SyncWriteFuture future = (SyncWriteFuture) SyncWriteMap.syncKey.get(requestId);
        if (future != null){
            future.setResponse(res);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
